package com.tanhua.dubbo.api.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.ObjectId;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.mapper.SettingsMapper;
import com.tanhua.dubbo.pojo.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author zmy  at 2021/6/21 23:14
 */
@DubboService(version = "1.0.0")
public class SettingsApiImpl implements SettingsApi {

    @Autowired
    private SettingsMapper settingsMapper;

    @Override
    public Settings queryById(Long userId) {

        QueryWrapper<Settings> settingsQueryWrapper = new QueryWrapper<>();
        settingsQueryWrapper.eq("user_id", userId);

        Settings settings = this.settingsMapper.selectOne(settingsQueryWrapper);
        if (settings == null) {
            return null;
        }
        return settings;
    }

    @Override
    public int notificationsSetting(Long userId, Object likeNotification, Object pinglunNotification, Object gonggaoNotification) {

        Settings settings = this.queryById(userId);
        settings.setLikeNotification(Convert.toInt(likeNotification));
        settings.setGonggaoNotification(Convert.toInt(gonggaoNotification));
        settings.setPinglunNotification(Convert.toInt(pinglunNotification));


        QueryWrapper<Settings> settingsQueryWrapper = new QueryWrapper<>();
        settingsQueryWrapper.eq("user_id", userId);

        int update = this.settingsMapper.update(settings, settingsQueryWrapper);
        System.out.println("update = " + update);

        return update;

    }
}
